<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('user', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$rols = $this->User->Rol->find('list');
		$schedulesOptions = $this->User->SchedulesOption->find('list');
		$this->set(compact('rols', 'schedulesOptions'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
		$rols = $this->User->Rol->find('list');
		$schedulesOptions = $this->User->SchedulesOption->find('list');
		$this->set(compact('rols', 'schedulesOptions'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user has been deleted.'));
		} else {
			$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}


	public function buscarIdProfesor($name){
		$nombre = explode(' ', $name);
		//echo $name;
		$p = "";
		$p1 = "";
		if(sizeof($nombre) == 2){
			if(!($p = $this->User->find('all', array(
				'conditions' => array(
					'User.first_name' => $nombre[0],
					'User.last_name' => $nombre[1]))))){
				echo ' profe fallo: '. $nombre[0].' '.$nombre[1];
				debug($this->User->validationErrors, true);
				return false;
			}
				
		}else{
			if(!($p = $this->User->find('all', array(
				'conditions' => array(
							'User.first_name' => $nombre[0].' '.$nombre[1],
							'User.last_name' => $nombre[2]
						), 
						
					)
				)
			) && !($p1 = $this->User->find('all', array(
				'conditions' => array(
							'User.first_name' => $nombre[0],
							'User.last_name' => $nombre[1].' '.$nombre[2]
						)
					)
				)
			)){
				echo ' profe fallo: '. $nombre[0].' '.$nombre[1].' '.$nombre[2];
				debug($this->User->validationErrors, true);
				return false;
			}else
				$p = (sizeof($p) >0)?$p:$p1;				
		}		
		return $p[0]['User']['id'];			
	}

}

?>

